//
//  RHMePriceCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/22.
//

#import "RHMePriceCell.h"
#import "RHMeElectricPricePatternModel.h"

@interface RHMePriceCell()<QMUITextFieldDelegate>

@property (nonatomic, strong) QMUILabel *titleLab;

@property (nonatomic, strong) QMUITextField *textField;
@end
@implementation RHMePriceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHMeElectricPricePatternModel *)model
{
    _model = model;
    _titleLab.text = [NSString stringWithFormat:@"*%@",model.title.rh_localized];
    _titleLab.attributedText = [NSString attributeStringWithContent:_titleLab.text attriFont:kSYSTEMFONT(14) attriColor:kCOLOR_HEX(@"#F95454") attriKey:@"*"];
    self.textField.text = [NSDecimalNumber decimalStringWithDoubleString:model.content nullStr:nil];
}
- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:bgView];
    self.bgView = bgView;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 15, 0, 15));
    }];
    
    _titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#474B53") textAligment:NSTextAlignmentLeft];
    
    [bgView addSubview:_titleLab];
    
    UIView *priceView = kView;
    [priceView rounded:5 width:1 color:kBorderColor];
    [bgView addSubview:priceView];
    [priceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.bottom.mas_offset(-10);
        make.right.mas_offset(-10);
        make.height.offset(34);
        
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(12);
        make.right.equalTo(priceView.mas_left).offset(-3);
        make.centerY.equalTo(priceView);
    }];
    
    QMUILabel *unitLab = [QMUILabel labelWithText:@"元/kWh".rh_localized font:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#8B8D92")];
    [priceView addSubview:unitLab];
    [unitLab sizeToFit];
    
    [unitLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-5);
        make.centerY.mas_offset(0);
        make.width.offset(unitLab.width+1);
    }];
    
    QMUITextField *textField = [[QMUITextField alloc]init];
    textField.font = kSYSTEMFONT(14);
    textField.textColor = kCOLOR_HEX(@"#303133");
    textField.textAlignment = NSTextAlignmentLeft;
    textField.textInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    textField.delegate = self;
    [priceView addSubview:textField];
    self.textField = textField;
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.right.equalTo(unitLab.mas_left).offset(-3);
        make.height.offset(34);
        make.width.offset(100);
        make.centerY.mas_offset(0);
    }];

    [textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        weakSelf.model.content = x;
    }];
    if ([self.reuseIdentifier isEqualToString:@"RHMePriceCell_detail"]) {
        textField.enabled = NO;
    }
}

-(BOOL)textField:(QMUITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //删除
    if ([string isEqualToString:@""]) {
        return YES;
    }
    
    if ([textField.text containsString:@"."]) {
        //有小数点就不让再次输入小数点
        if ([string containsString:@"."]||textField.text.length == textField.maximumTextLength) {
            return NO;
        }
        //只能输入两位小数
        //小数点的位置
        NSRange point_range = [textField.text rangeOfString:@"."];
        //在小数点前面可以输入
        if (range.location <= point_range.location) {
            return YES;
        }
        NSString *pointValue = [textField.text substringFromIndex:point_range.location+1];
        NSLog(@"------>%@",pointValue);
        if (pointValue.length>=3) {
            return NO;
        }
        return YES;
    }else{
        //没有小数点存在,如果第一个输入的是小数点,自动补0
        if ([string isEqualToString:@"."]&&textField.text.length == 0) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                textField.text = [NSString stringWithFormat:@"0%@",string];
            });
            return YES;
        }
    }
    
    return YES;
}
@end
